home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / what's new? / sample code / human interface toolbox / packagetool / sample package / htmlsample sources / htmlsample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-02  |  23.9 KB  |  775 lines

  1. /*
  2.     file HTMLSample.c
  3.     
  4.     Description:
  5.     This file contains the main application program for the HTMLSample.
  6.     Routines in this file are responsible for handling events directed
  7.     at the application.
  8.     
  9.     HTMLSample is an application illustrating how to use the new
  10.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  11.     is Apple's light-weight HTML rendering engine capable of
  12.     displaying HTML files.
  13.  
  14.     Copyright: © 1999 by Apple Computer, Inc.
  15.     all rights reserved.
  16.     
  17.     Disclaimer:
  18.     You may incorporate this sample code into your applications without
  19.     restriction, though the sample code has been provided "AS IS" and the
  20.     responsibility for its operation is 100% yours.  However, what you are
  21.     not permitted to do is to redistribute the source as "DSC Sample Code"
  22.     after having made changes. If you're going to re-distribute the source,
  23.     we require that you make it clear in the source that the code was
  24.     descended from Apple Sample Code, but that you've made changes.
  25.     
  26.     Change History (most recent first):
  27.     10/16/99 created
  28. */
  29.  
  30. #include "HTMLSample.h"
  31.  
  32. #include <Menus.h>
  33. #include <Windows.h>
  34. #include <Dialogs.h>
  35. #include <Events.h>
  36. #include <Fonts.h>
  37. #include <SegLoad.h>
  38. #include <Resources.h>
  39. #include <Balloons.h>
  40. #include <Devices.h>
  41. #include <AppleEvents.h>
  42. #include <StdIO.h>
  43. #include <StdArg.h>
  44. #include <string.h>
  45. #include <ToolUtils.h>
  46. #include <Appearance.h>
  47. #include <Navigation.h>
  48. #include <StandardFile.h>
  49.  
  50.  
  51. #include <HTMLRendering.h>
  52.  
  53. #include "RenderingWindow.h"
  54. #include "SampleUtils.h"
  55. #include "AboutBox.h"
  56.  
  57.     /* true while the app is running */
  58. Boolean gRunning = true;
  59.  
  60. #ifndef __MWERKS__
  61. QDGlobals    qd; /* QuickDraw globals */
  62. #endif
  63.  
  64.  
  65.  
  66. /* OpenOneFile is called for each file the application is asked to open
  67.     either by way of Apple event or from the file menu.  spec points to
  68.     a file specification record referring to the file to open.  The file will
  69.     be opened in a new window. */
  70. static OSErr OpenOneFile(FSSpec *spec) {
  71.     Handle urlHandle, errorPageLink;
  72.     WindowPtr rWindow;
  73.     Str255 errStr;
  74.     OSErr err;
  75.         /* initial state */
  76.     urlHandle = NULL;
  77.     rWindow = NULL;
  78.         /* allocate locals */
  79.     urlHandle = NewHandle(0);
  80.     if (urlHandle == NULL) { err = memFullErr; goto bail; }
  81.     errorPageLink = GetResource(kCStyleStringResourceType, kErrorPageURLString);
  82.     if (errorPageLink == NULL)  { err = resNotFound; goto bail; }
  83.         /* convert the fsspec to a url */
  84.     err = HRUtilGetURLFromFSSpec(spec, urlHandle);
  85.     if (err != noErr) goto bail;
  86.         /* open the window */
  87.     err = RWOpen(&rWindow);
  88.     if (err != noErr) goto bail;
  89.         /* attempt to open the url */
  90.     MoveHHi(urlHandle);
  91.     HLock(urlHandle);
  92.     err = RWGotoURL(rWindow, *urlHandle, true);
  93.     HUnlock(urlHandle);
  94.         /* if that fails, try to show the error page */
  95.     if (err != noErr) {
  96.         MoveHHi(errorPageLink);
  97.         HLock(errorPageLink);
  98.         err = RWGotoAppRelLink(rWindow, *errorPageLink, true);
  99.         HUnlock(errorPageLink);
  100.         if (err != noErr) goto bail;
  101.     }
  102.         /* clean up and leave */
  103.     DisposeHandle(urlHandle);
  104.     return noErr;
  105. bail:
  106.         /* we display an alert here if there's an error. returning
  107.         an error from this routine will abort any open routine that
  108.         is going on--even if we're in the middle of a list of files. */
  109.     NumToString(err, errStr);
  110.     ParamAlert(kOpenFileErrorAlert, errStr, spec->name);
  111.     if (rWindow != NULL) RWCloseWindow(rWindow);
  112.     if (urlHandle != NULL) DisposeHandle(urlHandle);
  113.     return err;
  114. }
  115.  
  116. /* IdentifyPackage identifies a Mac OS 9 package and returns a reference
  117.     to it's main file inside of mainPackageFile.  In Mac OS 9, packages are
  118.     the special folders that have their bundle bits set and contain an alias
  119.     at their topmost level referring to a file somewhere in the package. */
  120. static Boolean IdentifyPackage(FSSpec *target, FSSpec *mainPackageFile) {
  121.     CInfoPBRec cat;
  122.     OSErr err;
  123.     long pDir;
  124.     Str255 name;
  125.     FSSpec aliasFile;
  126.     Boolean targetIsFolder, wasAliased;
  127.         /* check the target's flags */
  128.     cat.dirInfo.ioNamePtr = target->name;
  129.     cat.dirInfo.ioVRefNum = target->vRefNum;
  130.     cat.dirInfo.ioFDirIndex = 0;
  131.     cat.dirInfo.ioDrDirID = target->parID;
  132.     err = PBGetCatInfoSync(&cat);
  133.     if (err != noErr) return false;
  134.         /* if it's a folder and the bundle bit is set....*/
  135.     if (((cat.dirInfo.ioFlAttrib & 16) != 0) && ((cat.dirInfo.ioDrUsrWds.frFlags & kHasBundle) != 0)) {
  136.             /* search for a top level alias file.  Here, we enumerate all of the
  137.             objects in the directory until we find a file with the alias flag set. */
  138.         pDir = cat.dirInfo.ioDrDirID;
  139.         cat.dirInfo.ioNamePtr = name;
  140.         cat.dirInfo.ioVRefNum = target->vRefNum;
  141.         cat.dirInfo.ioFDirIndex = 1;
  142.         cat.dirInfo.ioDrDirID = pDir;
  143.         while (PBGetCatInfoSync(&cat) == noErr) {
  144.                 /* if the thing we're looking at is not a directory and it's alias flag is set,
  145.                 try to resolve it as an alias file. */
  146.             if (((cat.dirInfo.ioFlAttrib & 16) == 0) && ((cat.dirInfo.ioDrUsrWds.frFlags & kIsAlias) != 0)) {
  147.                 err = FSMakeFSSpec(target->vRefNum, pDir, name, &aliasFile);
  148.                 if (err != noErr) return false;
  149.                 err = ResolveAliasFile(&aliasFile, false, &targetIsFolder, &wasAliased);
  150.                 if (err != noErr) return false;
  151.                 if (mainPackageFile != NULL)
  152.                     *mainPackageFile = aliasFile;
  153.                 return true;
  154.             }
  155.                 /* move on to the next file in the directory. */
  156.             cat.dirInfo.ioFDirIndex++;
  157.             cat.dirInfo.ioDrDirID = pDir;
  158.         }
  159.     }
  160.         /* we found nothing matching our criteria, so we
  161.         fail. */
  162.     return false;
  163. }
  164.  
  165.  
  166. /* OpenTheDocuments is called to open a list of documents provided by
  167.     either an open documents Apple event or one of the Navigation services
  168.     dialogs.  */
  169. static OSErr OpenTheDocuments(AEDescList *theDocuments) {
  170.     OSErr err;
  171.     long i, n;
  172.     FSSpec fileSpec, packageSpec;
  173.     AEKeyword keyWd;
  174.     DescType typeCd;
  175.     Size actSz;
  176.     
  177.         /* open them */
  178.     err = AECountItems(theDocuments, &n);
  179.     if (err != noErr) goto bail;
  180.         /* and then open each one */
  181.     for (i = 1 ; i <= n; i++) {
  182.             /* get the i'th FSSpec record.  NOTE: implicity, we are calling
  183.             a coercion handler because this list actually contains alias records. 
  184.             In particular, the coercion handler converts them from alias records
  185.             into FSSpec records. */
  186.         err = AEGetNthPtr(theDocuments, i, typeFSS, &keyWd, &typeCd,
  187.             (Ptr) &fileSpec, sizeof(fileSpec), (actSz = sizeof(fileSpec), &actSz));
  188.         if (err != noErr) goto bail;
  189.             /* if it's a package, we'll open it's main file, otherwise
  190.             we'll open the file itself */
  191.         if (IdentifyPackage(&fileSpec, &packageSpec))
  192.             err = OpenOneFile(&packageSpec);
  193.         else err = OpenOneFile(&fileSpec);
  194.         if (err != noErr) goto bail;
  195.     }
  196.     return noErr;
  197. bail:
  198.     return err;
  199. }
  200.  
  201.  
  202. /* MyNavFilterProc This is the filter function we provide for the Navigation services
  203.     dialogs.  We only allow files of type TEXT. */
  204. static pascal Boolean MyNavFilterProc( AEDesc* theItem, void* info, NavCallBackUserData callBackUD, NavFilterModes filterMode) {
  205.     NavFileOrFolderInfo* theInfo;
  206.     if ( theItem->descriptorType == typeFSS ) {
  207.         theInfo = (NavFileOrFolderInfo*) info;
  208.         if ( theInfo->isFolder ) return true;
  209.         if ( theInfo->fileAndFolder.fileInfo.finderInfo.fdType != 'TEXT' )
  210.             return false;
  211.     }
  212.     return true;
  213. }
  214.  
  215.  
  216. /* NavEventCallBack is the event handling call back we provide for Navigation
  217.     services.  It's presence is important so our windows will be updated appropriately
  218.     when the navigation window is resized or moved. */
  219. static pascal void NavEventCallBack( NavEventCallbackMessage callBackSelector,
  220.             NavCBRecPtr callBackParms, NavCallBackUserData callBackUD) {
  221.     if (callBackSelector == kNavCBEvent && callBackParms->eventData.eventDataParms.event->what == updateEvt) {
  222.  
  223.         HandleEvent(callBackParms->eventData.eventDataParms.event);
  224.  
  225.     }
  226. }
  227.  
  228.  
  229. /* MyFileFilterProc is used by the older standard file calls.  We fall back to
  230.     standard file when navigation services is not present or unavailable.  In this
  231.     routine, we filter out all invisible files. */
  232. static pascal Boolean MyFileFilterProc(CInfoPBPtr pb) {
  233.         /* don't display invisible files */
  234.     return ((pb->hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible) != 0);
  235. }
  236.  
  237.  
  238. /* SelectAndOpenFile is the inner workings of the Open... command
  239.     when it is chosen from the file menu.  Here, we use the navigation
  240.     services dialogs when they are available, but if they're not, then we
  241.     use the standard file ones. */
  242. static OSStatus SelectAndOpenFile(void) {
  243.     NavDialogOptions dialogOptions;
  244.     NavReplyRecord theReply;
  245.     NavEventUPP eventf;
  246.     NavObjectFilterUPP filterf;
  247.     FileFilterUPP stdFilterf;
  248.     Boolean hasreply;
  249.     OSStatus err;
  250.         /* set up locals */
  251.     eventf = NULL;
  252.     filterf = NULL;
  253.     stdFilterf = NULL;
  254.     hasreply = false;
  255.     memset(&theReply, 0, sizeof(theReply));
  256.         /* if Navigation services is available, then we
  257.         use those calls. */
  258.     if (NavServicesAvailable()) {
  259.             /* allocate data */
  260.         filterf = NewNavObjectFilterUPP(MyNavFilterProc);
  261.         if (filterf == NULL) { err = memFullErr; goto bail; }
  262.         eventf = NewNavEventProc(NavEventCallBack);
  263.         if (eventf == NULL) { err = memFullErr; goto bail; }
  264.             /* set up dialog options */
  265.         err = NavGetDefaultDialogOptions(&dialogOptions);
  266.         if (err != noErr) goto bail;
  267.             /* NOTE: by setting the kNavAllowMultipleFiles flag, we make it possible
  268.             for the user to select more than one file.  And, setting the kNavSupportPackages
  269.             flag allows us to open package documents. */
  270.         dialogOptions.dialogOptionFlags = (kNavDontAutoTranslate | kNavAllowMultipleFiles | kNavSupportPackages);
  271.         GetIndString(dialogOptions.message, kMainStringList, kNavMessageString);
  272.             /* pick one or more files */
  273.         err = NavChooseFile(NULL, &theReply, &dialogOptions, eventf,  NULL,  filterf,  NULL, NULL);
  274.         if (err != noErr) goto bail;
  275.         if (!theReply.validRecord) { err = userCanceledErr; goto bail; }
  276.         hasreply = true;
  277.             /* if we have a valid reply, then call our
  278.             open documents routine. */
  279.         err = OpenTheDocuments(&theReply.selection);
  280.         if (err != noErr) goto bail;
  281.             /* clean up the structures we allocated */
  282.         NavDisposeReply(&theReply);
  283.         DisposeNavEventUPP(eventf);
  284.         DisposeNavObjectFilterUPP(filterf);
  285.     } else {
  286.         StandardFileReply reply;
  287.         SFTypeList typeList = { 'TEXT', 'TEXT', 'TEXT', 'TEXT' };
  288.             /* set up our locals */
  289.         stdFilterf = NewFileFilterUPP(MyFileFilterProc);
  290.         if (stdFilterf == NULL) { err = memFullErr; goto bail; }
  291.             /* ask for a file.  NOTE: with standard file
  292.             we can only get one file at a time. */
  293.         StandardGetFile(stdFilterf, 1, typeList, &reply);
  294.         if ( ! reply.sfGood) { err = userCanceledErr; goto bail; }
  295.             /* if a file was chosen, open it. */
  296.         err = OpenOneFile(&reply.sfFile);
  297.         if (err != noErr) goto bail;
  298.             /* clean up the structures we allocated */
  299.         DisposeFileFilterUPP(stdFilterf);
  300.     }
  301.     return noErr;
  302. bail:
  303.     if (hasreply) NavDisposeReply(&theReply);
  304.     if (eventf != NULL) DisposeNavEventUPP(eventf);
  305.     if (filterf != NULL) DisposeNavObjectFilterUPP(filterf);
  306.     if (stdFilterf != NULL) DisposeFileFilterUPP(stdFilterf);
  307.     return err;
  308. }
  309.  
  310.  
  311.  
  312.  
  313. /* ResetMenus is called immediately before all calls to
  314.     MenuSelect or MenuKey.  In this routine, we re-build
  315.     or enable the menus as appropriate depending on the
  316.     current environment */
  317. static void ResetMenus(void) {
  318.     WindowPtr target;
  319.     target = FrontWindow();
  320.         /* if the front most window is a rendering
  321.         window, then we let the window handle the
  322.         menu. */
  323.     if (IsARenderingWindow(target))
  324.         RWResetGotoMenu(target);
  325.     else {
  326.         MenuHandle goMenu;
  327.             /* otherwise, we clear the menu
  328.             and disable all of its commands. */
  329.         goMenu = GetMenuHandle(mGo);
  330.         DisableItem(goMenu, iBack);
  331.         DisableItem(goMenu, iForward);
  332.         DisableItem(goMenu, iHome);
  333.         while (CountMItems(goMenu) >= iGoSep)
  334.             DeleteMenuItem(goMenu, iGoSep);
  335.     }
  336. }
  337.  
  338.  
  339. /* DoMenuCommand is called in response to MenuKey
  340.     or MenuSelect.  Here, we dispatch the menu command
  341.     to its appropriate handler, or if it's a small action
  342.     we do it here. */
  343. static void DoMenuCommand(long rawMenuSelectResult) {
  344.     short menu, item;
  345.         /* decode the MenuSelect result */
  346.     menu = (rawMenuSelectResult >> 16);
  347.     if (menu == 0) return;
  348.     item = (rawMenuSelectResult & 0x0000FFFF);
  349.         /* dispatch on result */
  350.     switch (menu) {
  351.             /* apple menu commands */
  352.         case mApple:
  353.             if (item == iAbout) {
  354.                 WindowPtr aboutBox;
  355.                 OSStatus err;
  356.                     /* open the about box */
  357.                 err = OpenAboutBox(&aboutBox);
  358.                 if (err != noErr) {
  359.                     Str255 errStr;
  360.                     NumToString(err, errStr);
  361.                     ParamAlert(kNoAboutBoxErrorAlert, errStr, NULL);
  362.                 }
  363.             } else if (item >= iFirstAppleItem) {
  364.                     /* if the item selected is below the separator
  365.                     line, then we open it as a desk accessory. */
  366.                 Str255 deskAccName;
  367.                 GetMenuItemText(GetMenuHandle(mApple), item, deskAccName);
  368.                 OpenDeskAcc(deskAccName);
  369.             }
  370.             break;
  371.             
  372.                 /* file menu commands */
  373.         case mFile:
  374.             if (item == iOpen) {
  375.                 SelectAndOpenFile();
  376.             } else if (item == iQuit) {
  377.                 if (CloseRenderingWindows() == noErr)
  378.                     gRunning = false;
  379.             }
  380.             break;
  381.             
  382.                 /* selections in the Go menu are handled by
  383.                 the frontmost rendering window. */
  384.         case mGo:
  385.             {    WindowPtr target;
  386.                 target = FrontWindow();
  387.                 if (IsARenderingWindow(target))
  388.                     RWHandleGotoMenu(target, item);
  389.             }
  390.             break;
  391.  
  392.     }
  393.         /* unhilite the menu bar */
  394.     HiliteMenu(0);
  395. }
  396.  
  397.  
  398. /* QuitAppleEventHandler is our quit Apple event handler.  this routine
  399.     is called when a quit Apple event is sent to our application.  Here,
  400.     we set the gRunning flag to false. NOTE:  it is not appropriate to
  401.     call ExitToShell here.  Instead, by setting the flag to false we
  402.     fall through the bottom of our main loop the next time we're called. 
  403.     Here, if we are unable to close all of the rendering windows,
  404.     we return an error.  This will abort the shutdown process,
  405.     if that's why we were called.  */
  406. static pascal OSErr QuitAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
  407.     if (CloseRenderingWindows() == noErr) {
  408.         gRunning = false;
  409.         return noErr;
  410.     } else return userCanceledErr;
  411. }
  412.  
  413.  
  414. /* OpenAppleEventHandler is called when our application receives
  415.     an 'open application' apple event.  Here, we put up a window
  416.     referring to the default page. */
  417. static pascal OSErr OpenAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
  418.     WindowPtr rWin;
  419.     Handle urlHandle;
  420.     OSErr err;
  421.         /* creat a rendering window. */
  422.     err = RWOpen(&rWin);
  423.     if (err != noErr) return err;
  424.         /* get the link to the default page from the resource
  425.         file. */
  426.     urlHandle = GetResource(kCStyleStringResourceType, kDefaultPageURLString);
  427.     if (urlHandle == NULL) return memFullErr;
  428.         /* lock down the resource and point the rendering
  429.         window at it. */
  430.     MoveHHi(urlHandle);
  431.     HLock(urlHandle);
  432.     RWGotoAppRelLink(rWin, *urlHandle, true);
  433.     HUnlock(urlHandle);
  434.         /* done */
  435.     return noErr;
  436. }
  437.  
  438.  
  439. /* ReOpenAppleEventHandler is called whenever the application receives
  440.     a re-open Apple event.  This will happen if the application is already
  441.     running and the user attempts to open it again by either double clicking
  442.     on its icon in the Finder or by selecting its icon and choosing Open in
  443.     the Finder's file menu. Here, if there is no window showing, then we
  444.     open a new one as we would if an open application event was received. */
  445. static pascal OSErr ReOpenAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
  446.     
  447.     if (FrontWindow() == NULL)
  448.         return OpenAppleEventHandler(appleEvt, reply, refcon);
  449.     else return noErr;
  450.     
  451.     return noErr;
  452. }
  453.  
  454.  
  455. /* OpenDocumentsEventHandler is called whenever we receive an open documents
  456.     Apple event.  Here, we extract the list of documents from the event
  457.     and pass them along to our OpenTheDocuments routine. */
  458. static pascal OSErr OpenDocumentsEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, long refcon) {
  459.     OSErr err;
  460.     AEDescList documents;
  461.     
  462.         /* initial state */
  463.     AECreateDesc(typeNull, NULL, 0, &documents);
  464.     
  465.         /* get the open parameter */
  466.     err = AEGetParamDesc(appleEvt, keyDirectObject, typeAEList, &documents);
  467.     if (err != noErr) goto bail;
  468.     
  469.         /* open the documents */
  470.     err = OpenTheDocuments(&documents);
  471.     if (err != noErr) goto bail;
  472.  
  473. bail:
  474.     AEDisposeDesc(&documents);
  475.     return err;
  476. }
  477.  
  478.  
  479.  
  480.  
  481.  
  482. /* HandleMouseDown is called for mouse down events.  Processing of
  483.     mouse down events in the HTML rendering area of windows is 
  484.     handled by the HTMLRenderinLib, but clicks in the controls and
  485.     other parts of the windows are handled here. */
  486. static void HandleMouseDown(EventRecord *ev) {
  487.     WindowPtr theWindow;
  488.     short partcode;
  489.     partcode = FindWindow(ev->where, &theWindow);
  490.     switch (partcode) {
  491.             /* inside the window's content area */
  492.         case inContent:
  493.             if (theWindow != FrontWindow()) {
  494.                     /* if it's not the frontmost window,
  495.                     then make it the frontmost window. */
  496.                 SelectWindow(theWindow);
  497.             } else {
  498.                     /* otherwise, if it's a rendering window,
  499.                     pass the click along to the window. */
  500.                 Point where;
  501.                 SetPort(theWindow);
  502.                 SetOrigin(0, 0);
  503.                 where = ev->where;
  504.                 GlobalToLocal(&where);
  505.                 if (IsARenderingWindow(theWindow))
  506.                     RWHandleMouseDown(theWindow, where);
  507.             }
  508.             break;
  509.             
  510.             /* menu bar clicks */
  511.         case inMenuBar:
  512.             ResetMenus();
  513.             DoMenuCommand(MenuSelect(ev->where));
  514.             break;
  515.             
  516.             /* track clicks in the close box */
  517.         case inGoAway:
  518.             if (TrackGoAway(theWindow, ev->where)) {
  519.                 if (IsARenderingWindow(theWindow))
  520.                     RWCloseWindow(theWindow);
  521.                 else if (IsAboutBox(theWindow))
  522.                     AboutBoxCloseWindow(theWindow);
  523.             }
  524.             break;
  525.             
  526.             /* allow window drags */
  527.         case inDrag:
  528.             {    Rect boundsRect = {0,0, 32000, 32000};
  529.                 DragWindow(theWindow, ev->where, &boundsRect);
  530.             }
  531.             break;
  532.             
  533.             /* allow window drags */
  534.         case inGrow:
  535.             {    Rect sizerect;
  536.                 long grow_result;
  537.                 SetRect(&sizerect, 300, 150, 32767, 32767);
  538.                 grow_result = GrowWindow(theWindow, ev->where, &sizerect);
  539.                 if (grow_result != 0) {
  540.                     SizeWindow(theWindow, LoWord(grow_result), HiWord(grow_result), true);
  541.                     SetPort(theWindow);
  542.                     InvalRect(&theWindow->portRect);
  543.                     if (IsARenderingWindow(theWindow))
  544.                         RWRecalculateSize(theWindow);
  545.  
  546.                 }
  547.             }
  548.             break;
  549.             
  550.             /* zoom box clicks.  NOTE:  since the rendering window
  551.             always sets the standard rectangle to the 'best size' for
  552.             displaying the current HTML window, the inZoomOut partcode
  553.             will zoom the window to that size rather than the entire screen.*/
  554.         case inZoomIn:
  555.         case inZoomOut:
  556.             SetPort(theWindow);
  557.             EraseRect(&theWindow->portRect);
  558.             ZoomWindow(theWindow, partcode, true);
  559.             SetPort(theWindow);
  560.             if (IsARenderingWindow(theWindow))
  561.                 RWRecalculateSize(theWindow);
  562.             break;
  563.             
  564.             /* desktop clicks, etc... */
  565.         case inSysWindow:
  566.             SystemClick(ev, theWindow);
  567.             break;
  568.     }
  569. }
  570.  
  571.  
  572. /* HandleEvent is the main event handling routine for the
  573.     application.  ev points to an event record returned by
  574.     WaitNextEvent. */
  575. void HandleEvent(EventRecord *ev) {
  576.     WindowPtr target;
  577.         /* process menu key events */
  578.     if (((ev->what == keyDown) || (ev->what == autoKey)) && ((ev->modifiers & cmdKey) != 0)) {
  579.         ResetMenus();
  580.         DoMenuCommand(MenuKey((char) (ev->message & charCodeMask)));
  581.         ev->what = nullEvent;
  582.     }
  583.     
  584.         /* process HR events.  NOTE: this call handles most of the events
  585.         for the active HTML rendering object.  But, be careful it may set
  586.         the clip region and origin of that window to an unknown state. */
  587.     if (HRIsHREvent(ev))
  588.         ev->what = nullEvent;
  589.     
  590.         /* process other event types */
  591.     switch (ev->what) {
  592.             /* the application may be switching in to the forground
  593.             or into the background.  Either way, we need to activate
  594.             the frontmost window accordingly. */
  595.         case osEvt:
  596.             target = FrontWindow();
  597.             if (IsARenderingWindow(target))
  598.                 RWActivate(target, ((ev->message & resumeFlag) != 0));
  599.             else if (IsAboutBox(target))
  600.                 AboutBoxActivate(target, ((ev->message & resumeFlag) != 0));
  601.             break;
  602.             
  603.             /* for activate events we call the window's activate event
  604.             handler. */
  605.         case activateEvt:
  606.             target = (WindowPtr) ev->message;
  607.             if (IsARenderingWindow(target))
  608.                 RWActivate(target, ((ev->modifiers&1) != 0));
  609.             else if (IsAboutBox(target))
  610.                 AboutBoxActivate(target, ((ev->modifiers&1) != 0));
  611.             break;
  612.             
  613.             /* for update events we call the window's update event
  614.             handler. if the window is of an unknown type, we ignore the
  615.             event. */
  616.         case updateEvt:
  617.             target = (WindowPtr) ev->message;
  618.             if (IsARenderingWindow(target))
  619.                 RWUpdate(target);
  620.             else if (IsAboutBox(target))
  621.                 AboutBoxUpdate(target);
  622.             else {
  623.                 BeginUpdate(target);
  624.                 EndUpdate(target);
  625.             }
  626.             break;    
  627.             
  628.             /* for mouse events we call the the HandleMouseDown routine
  629.             defined above. */
  630.         case mouseDown:
  631.             HandleMouseDown(ev);
  632.             break;
  633.         
  634.             /* for key down events we call the window's key down event
  635.             handler. */
  636.         case keyDown:
  637.         case autoKey:
  638.             target = FrontWindow();
  639.             if (IsARenderingWindow(target))
  640.                 RWKeyDown(target, (char) (ev->message & charCodeMask));
  641.             break;
  642.         
  643.             /* Apple events. */
  644.         case kHighLevelEvent:
  645.             AEProcessAppleEvent(ev);
  646.             break;
  647.     }
  648. }
  649.  
  650.  
  651. /* FDPIdleProcedure is the idle procedure called by AEInteractWithUser while we are waiting
  652.     for the application to be pulled into the forground.  It simply passes the event along
  653.     to HandleNextEvent */
  654. static pascal Boolean MyIdleInteractProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn) {
  655.     HandleEvent(theEvent);
  656.     return ( ! gRunning ); /* quit waiting if we're not running */
  657. }
  658.  
  659.  
  660. /* ParamAlert is a general alert handling routine.  If Apple events exist, then it
  661.     calls AEInteractWithUser to ensure the application is in the forground, and then
  662.     it displays an alert after passing the s1 and s2 parameters to ParamText. */
  663. short ParamAlert(short alertID, StringPtr s1, StringPtr s2) {
  664.     AEIdleUPP aeIdleProc;
  665.     OSStatus err;
  666.     aeIdleProc = NewAEIdleUPP(MyIdleInteractProc);
  667.     if (aeIdleProc == NULL) { err = memFullErr; goto bail; }
  668.     err = AEInteractWithUser(kNoTimeOut, NULL, aeIdleProc);
  669.     if (err != noErr) goto bail;
  670.     ParamText(s1, s2, NULL, NULL);
  671.     err = Alert(alertID, NULL);
  672.     DisposeAEIdleUPP(aeIdleProc);
  673.     return err;
  674. bail:
  675.     if (aeIdleProc != NULL) DisposeAEIdleUPP(aeIdleProc);
  676.     return err;
  677. }
  678.  
  679.  
  680. /* MyGrowZone is called by the Memory Manager whenever it
  681.     cannot fulfil a memory request.  Here, we try to get back
  682.     enough memory for the request by asking the HTML rendering
  683.     library to free up some cache space. */
  684. static pascal long MyGrowZone(Size cbNeeded) {
  685.     return HRFreeMemory(cbNeeded);
  686. }
  687.  
  688.  
  689.  
  690.     /* the main program */
  691.  
  692. int main(void) {
  693.     OSStatus err;
  694.     Boolean isAppearanceClient;
  695.     Str255 errStr;
  696.     
  697.     isAppearanceClient = false;
  698.     
  699.         /* set up the macintosh managers */
  700.     SetApplLimit(GetApplLimit());
  701.     MaxApplZone();
  702.     InitGraf(&qd.thePort);
  703.     InitFonts();
  704.     InitWindows();
  705.     TEInit();
  706.     InitMenus();
  707.     InitDialogs(0);
  708.     FlushEvents(everyEvent, 0);
  709.     InitCursor();
  710.     
  711.         /* register ourselves with the appearance manager. */
  712.     err = RegisterAppearanceClient();
  713.     if (err != noErr) goto bail;
  714.     isAppearanceClient = true;
  715.         
  716.         /* install our event handlers */
  717.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(OpenAppleEventHandler), 0, false);
  718.     if (err != noErr) goto bail;
  719.     err = AEInstallEventHandler(kCoreEventClass, 'rapp', NewAEEventHandlerProc(ReOpenAppleEventHandler), 0, false);
  720.     if (err != noErr) goto bail;
  721.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(OpenDocumentsEventHandler), 0, false);
  722.     if (err != noErr) goto bail;
  723.     err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(QuitAppleEventHandler), 0, false);
  724.     if (err != noErr) goto bail;
  725.  
  726.         /* set up the menu bar */
  727.     SetMenuBar(GetNewMBar(kMenuBarID));
  728.     DrawMenuBar();
  729.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');
  730.  
  731.         /* set up the rendering library */
  732.     if ( ! HRHTMLRenderingLibAvailable()) {
  733.         ParamAlert(kNoRenderingLibErrorAlert, NULL, NULL);
  734.         err = userCanceledErr;
  735.         goto bail;
  736.     }
  737.     
  738.         /* install our memory manger grow zone
  739.         routine. */
  740.     SetGrowZone(NewGrowZoneUPP(MyGrowZone));
  741.     
  742.         /* initialize the rendering windows library */
  743.     err = InitRenderingWindows();
  744.     if (err != noErr) goto bail;
  745.     
  746.         /* run the app */
  747.     while (gRunning) {
  748.         EventRecord ev;
  749.         
  750.             /* get the next event */
  751.         if ( ! WaitNextEvent(everyEvent, &ev,  GetCaretTime(), NULL))
  752.             ev.what = nullEvent;
  753.             
  754.             /* call our handler to deal with it. */
  755.         HandleEvent(&ev);
  756.  
  757.     }
  758.     
  759.         /* close all of our windows. */
  760.     CloseRenderingWindows();
  761.     EnsureAboutBoxIsClosed();
  762.     
  763.         /* unregister ourselves with the appearance manager. */
  764.     UnregisterAppearanceClient();
  765.     ExitToShell();
  766.     return 0;
  767. bail:
  768.     NumToString(err, errStr);
  769.     if (err != userCanceledErr)
  770.         ParamAlert(kOpenApplicationErrorAlert, errStr, NULL);
  771.     if (isAppearanceClient)
  772.         UnregisterAppearanceClient();
  773.     ExitToShell();
  774.     return 0;
  775. }